package com.my.blogapi.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.my.blogapi.dao.mapper.SysUserMapper;
import com.my.blogapi.dao.pojo.SysUser;
import com.my.blogapi.service.SysUserService;
import com.my.blogapi.utils.JWTUtils;
import com.my.blogapi.vo.UserVo;
import com.my.blogapi.vo.param.Result;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;

@Service
public class SysUserServiceImpl implements SysUserService {
    @Resource
    private SysUserMapper sysUserMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public SysUser findSysUser(String account, String password) {
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getAccount,account);
        queryWrapper.eq(SysUser::getPassword,password);
        queryWrapper.select(SysUser::getId,SysUser::getNickname,SysUser::getAccount,SysUser::getAvatar);
        queryWrapper.last("limit 1");
        return sysUserMapper.selectOne(queryWrapper);
    }

    /**
     * 根据Token获取用户信息
     * @param token
     * @return
     */
    @Override
    public SysUser findSysUserByToken(String token) {
        //判断token是否为空
        if (StringUtils.isBlank(token)){
            return null;
        }
        //检测Token的合法性
        Map<String, Object> map = JWTUtils.checkToken(token);
        if (map==null){
            return null;
        }
        String userJson = (String) redisTemplate.opsForValue().get("Token_" + token);
        if (userJson==null){
            return null;
        }
        SysUser sysUser = JSON.parseObject(userJson, SysUser.class);
        return sysUser;
    }

    /**
     * 根据用户名查询用户是否存在
     * @param account
     * @return
     */
    @Override
    public SysUser findSysUserByAccount(String account) {
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getAccount,account);
        queryWrapper.last("limit 1");
        SysUser sysUser = sysUserMapper.selectOne(queryWrapper);
        return sysUser;
    }

    /**
     * 注册
     * @param sysUser
     * @return
     */
    @Override
    public Result save(SysUser sysUser) {
        sysUserMapper.insert(sysUser);
        return Result.success(null);
    }

    @Override
    public SysUser findSysUserByAuthorId(Long authorId) {
        SysUser sysUser = sysUserMapper.selectById(authorId);
        if (sysUser==null){
            SysUser sysUserNew = new SysUser();
            sysUserNew.setAccount("我是作者鸭");
            return sysUserNew;
        }
        return sysUser;
    }

    /**
     * 根据id返回对应的UserVo信息
     * @param authorId
     * @return
     */
    @Override
    public UserVo findSysUserVoByAuthorId(Long authorId) {
        SysUser sysUser = sysUserMapper.selectById(authorId);
        UserVo userVo = new UserVo();
        if (sysUser==null){
            sysUser = new SysUser();
            sysUser.setId(1L);
            sysUser.setAvatar("/static/img/logo.b3a48c0.png");
            sysUser.setNickname("码神之路");
        }
        BeanUtils.copyProperties(sysUser,userVo);
        return userVo;
    }
}
